﻿using Newtonsoft.Json;
using OrBitDataModule.Common;
using OrBitDataModule.Models;
using OrBitDataModule.UI.Base;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OrBitDataModule.UI.Setting
{
    public partial class IntervalFolderWatcherSetting : SettingForm
    {
        public IntervalFolderWatcherSetting()
        {
            InitializeComponent();
            this.Load += IntervalFolderWatcherSetting_Load;
        }

        private void IntervalFolderWatcherSetting_Load(object sender, EventArgs e)
        {
            this.Text = SettingName;
            this.cb_fileEncoding.Items.AddRange(StringUtils.GetAllEncodingNames().ToArray());
            this.btn_watchPathSelect.Click += Btn_watchPathSelect_Click;
            this.btn_watchBackUpPathSelect.Click += Btn_watchBackUpPathSelect_Click;
            this.cb_fileType.Items.Add("文本");
            this.cb_fileType.Items.Add("Excel");
            this.cb_fileType.Items.Add("CSV");
        }

        public override string SettingName => "定时读取文件夹";
        public override string SettingType => "IntervalFolderWatcher";
        public override string GetSetting()
        {
            IntervalFolder intervalFolder = new IntervalFolder
            {
                WatchPath = this.tb_watchPath.Text,
                IsWatchSubDir = cb_isWatchSubDir.Checked,
                FileExtensions = this.tb_fileExtensions.Text,
                Encoding = this.cb_fileEncoding.Text,
                ReadFileInterval = (int)this.nud_Interval.Value,
                FileType = cb_fileType.Text,
                BackupFilePath = this.tb_backupFilePath.Text,

            };
            if (string.IsNullOrWhiteSpace(intervalFolder.WatchPath))
            {
                throw new Exception("监控目录不能为空");
            }
            //if (string.IsNullOrWhiteSpace(intervalFolder.BackupFilePath))
            //{
            //    throw new Exception("备份转移目录不能为空");
            //}
            if (string.IsNullOrWhiteSpace(intervalFolder.Encoding))
            {
                throw new Exception("文件编码不能为空");
            }
            if (!FileUtils.IsValidFilterFormat(intervalFolder.FileExtensions))
            {
                throw new Exception("文件后缀不符合规则，多个后缀请使用|分割");
            }
            if (intervalFolder.ReadFileInterval <= 0)
            {
                throw new Exception("定时间隔时间不能小于或者等于0");
            }
            return intervalFolder.ToString();
        }
        public override void LoadSetting(string json)
        {
            IntervalFolder intervalFolder = JsonConvert.DeserializeObject<IntervalFolder>(json);
            if (intervalFolder != null)
            {
                this.tb_watchPath.Text = intervalFolder.WatchPath;
                this.cb_isWatchSubDir.Checked = intervalFolder.IsWatchSubDir;
                this.nud_Interval.Value = intervalFolder.ReadFileInterval;
                this.tb_fileExtensions.Text = intervalFolder.FileExtensions;
                this.cb_fileType.Text = intervalFolder.FileType;
                this.cb_fileEncoding.Text = intervalFolder.Encoding;
                this.tb_backupFilePath.Text = intervalFolder.BackupFilePath;
            }
        }
        private void Btn_watchPathSelect_Click(object sender, EventArgs e)
        {
            using (FolderBrowserDialog folderBrowser = new FolderBrowserDialog())
            {
                if (folderBrowser.ShowDialog() == DialogResult.OK)
                {
                    this.tb_watchPath.Text = folderBrowser.SelectedPath;
                }
            }
        }

        private void Btn_watchBackUpPathSelect_Click(object sender, EventArgs e)
        {
            using (FolderBrowserDialog folderBrowser = new FolderBrowserDialog())
            {
                if (folderBrowser.ShowDialog() == DialogResult.OK)
                {
                    this.tb_backupFilePath.Text = folderBrowser.SelectedPath;
                }
            }
        }
    }
}
